class Node(object):
    def __init__(self, name, id, birth, hobby, path=None):
        self.name = name
        self.birth = birth
        self.hobby = hobby
        self.id = id
        self.path = path
        self.next = None
        self.pre = None
    def disp(self):
        print("姓名：{} \n学号：{}\n生日：{}\n兴趣：{}".format(self.name, self.id, self.birth,
                                                   self.hobby))

class STUlink(object):
    def __init__(self):
        self.header = Node(None, None, None, None)
        self.length = 0

    def is_empty(self):
        if self.header.next == None:
            return True
        else:
            return False

    def add(self, node):
        if self.is_empty():
            self.header.next = node
            node.pre = self.header
        else:
            node.next = self.header.next
            self.header.next.pre = node
            self.header.next = node
            node.pre = self.header
        self.length += 1

    def append(self, node):
        current_node = self.header
        if self.is_empty():
            self.add(node)
        else:
            while(current_node.next != None):
                current_node = current_node.next
            current_node.next = node
            node.pre = current_node
            self.length += 1

    def insert(self, node, index):#指定位置插入
        current_node = self.header.next

        if index >self.length + 1 or index <= 0:
            while(index > self.length + 1 or index <=0):
                print("你要插入的位置不对，请重选位置：")
                index = eval(input())

        if index == 1:
            self.add(node)
        elif index == 2:
            node.next = self.header.next.next
            self.header.next.next.pre = node
            self.header.next.next = node
            node.pre = self.header.next
            self.length += 1
        else:
            for i in range(1, index-1):
                current_node = current_node.next

            if current_node.next == None:
                current_node.next = node
                node.pre = current_node
            else:
                node.next = current_node.next
                current_node.next.pre = node
                current_node.next = node
                node.pre = current_node
            self.length += 1

    def travel(self):
        current_node = self.header.next
        if self.length == 0:
            print("目前链表中没有元素！")
        else:
            print("目前链表中有元素：")
            for i in range(self.length):
                print("姓名：{} \n学号：{}\n生日：{}\n兴趣：{}".format(current_node.name, current_node.id, current_node.birth,
                                                           current_node.hobby))
                current_node = current_node.next

    def searchNodeByIndex(self, index):
        current_Node = self.header.next
        if index <= 0 or index > self.length:
            while (index <= 0 or index > self.length):
                print("你输入的下标不对，请重新输入:")
                index = eval(input())
            #   return 0
        if index > 0 or index <= self.length:
            for i in range(index - 1):
                current_Node = current_Node.next
            return current_Node

    def searchNodeByid(self, id):
        current_node = self.header.next
        if self.is_empty():
            return None
        else:
            while(current_node != None):
                if current_node.id == id:
                    break
                current_node = current_node.next
            return current_node

    def sort_id(self):
        for i in range(0, self.length - 1):
            current_Node = self.header.next
            for j in range(0, self.length - i - 1):
                if current_Node.id > current_Node.next.id:
                    temp = current_Node.next
                    current_Node.next = temp.next
                    current_Node.pre.next = temp
                    temp.next = current_Node
                    temp.pre = current_Node.pre
                    current_Node.pre = temp
                    if current_Node.next != None:
                        current_Node.next.pre = current_Node
                else:
                    current_Node = current_Node.next

    def save_data(self):
        current_node = self.header.next
        f = open(r"C:\Users\86183\PycharmProjects\link\studentdata.txt", 'w')
        while(current_node !=None):
            data = current_node.name + " " + str(current_node.id) + " " + current_node.birth + " " + current_node.hobby+' '+current_node.path
            f.write(data)
            f.write('\n')
            current_node = current_node.next
        f.close()

    def load_data(self):
        self.header.next = None
        self.length = 0
        f = open(r"C:\Users\86183\PycharmProjects\link\studentdata.txt", "r")
        for line in f:
            data = (list(line.strip('\n').split(' ')))
            node = Node(data[0], data[1], data[2], data[3], data[4])
            self.append(node)

    def cancle_id(self, id):
        find = self.searchNodeByid(id)
        if find == None:
            pass
        else:
            if find.next == None:
                find.pre.next = None
            else:
                find.pre.next = find.next
                find.next.pre = find.pre
            self.length -= 1
